package com.example.employeeserver.service;

import com.example.employeeserver.dao.EmployeeDao;
import com.example.employeeserver.entity.EmployeeEntity;
import com.example.employeeserver.feign.SalaryFeignClient;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class EmployeeService implements IEmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Resource
    private SalaryFeignClient salaryFeignClient;

    // @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
    @GlobalTransactional
    public String join(EmployeeEntity employeeEntity) {
        // 第一步：插入员工基础信息
        employeeDao.insertEmployee(employeeEntity);
        // 第二步：调用员工薪资微服务，插入员工薪资
        salaryFeignClient.saveSalary(employeeEntity.getId(), employeeEntity.getSalary());

        int i = 1 / 0;
        return "员工入职成功";
    }

    public String confirm(EmployeeEntity employeeEntity){
        log.info("来confirm");
        // 修改员工的状态为已生效
        employeeDao.updateEmployeeStatus(employeeEntity.getId());
        return "confirm";
    }

    public String cancel(EmployeeEntity employeeEntity){
        // 删除员工数据
        log.info("来cancel");
        employeeDao.deleteEmployee(employeeEntity.getId());
        return "cancel";
    }

}
